// src/types/userTypes.ts

export interface UserData {
  id: string;
  username: string;
  mode: string;
  account: string;
  name: string;
  email: string;
  token: string;
  accessToken: any;
  // 根据实际需求添加其他字段
}

export interface UserLoginData {
  username: string;
  password: string;
  loginType: string;
  autoLogin: boolean;
  phone: string;
  verificationCode: string;
}

export interface UserResetPwdData {
  username: string;
  email: string;
  verificationCode: string;
  newPassword: string;
  confirmNewPassword: string;
}
// 根据实际需求添加其他字段

// 定义 formState 的类型
export interface FormState {
  username: string;
  email: string;
  password: string;
  confirmPassword: string;
}

export interface ResetPwdFormState {
  username: string;
  email: string;
}

// Vuex Store 相关的类型定义
export interface State {
  userInfo: UserData | null;
  isLoggedIn: boolean;
  isRegistering: boolean;
  isForgotPassword: boolean;
  accessToken: string | null; // 添加 accessToken 字段
  formState: FormState; // 添加 formState 字段
  resetPwdFormState: ResetPwdFormState; // 添加 resetPwdFormState 字段
}

export type Mutations<S = State> = {
  setUser(state: S, userData: UserData): void;
  setIsLoggedIn(state: S, isLoggedIn: boolean): void;
  setIsRegistering(state: S, isRegistering: boolean): void;
  setIsForgotPassword(state: S, isForgotPassword: boolean): void;
  setAccessToken(state: S, token: string): void; // 添加 setAccessToken 变更
  clearUser(state: S): void;
  updateFormState(state: S, payload: Partial<FormState>): void; // 更新 formState 的 mutation
  clearFormState(state: S): void; // 清空 formState 的 mutation
  clearResetPwdFormState(state: S): void; // 清空 resetPwdFormState 的 mutation
};

export type Actions = {
  login(
    context: { commit: Function },
    payload: { userData: UserData; token: string }
  ): void; // 修改为接受一个包含 userData 和 token 的对象
  logout(context: { commit: Function }): void;
  registerUser(
    context: { commit: Function },
    payload: { userData: UserData }
  ): void;
  resetPassword(
    context: { commit: Function },
    payload: { userData: UserResetPwdData }
  ): void;
  fetchAccessToken(context: { commit: Function }): Promise<void>; // fetchAccessToken 返回 Promise
};

export type Getters = {
  getUserInfo(state: State): UserData | null;
  isLoggedIn(state: State): boolean;
  getAccessToken(state: State): string | null; // 添加获取 accessToken 的 Getter
  getFormState(state: State): FormState; // 添加获取 formState 的 Getter
  getResetPwdFormState(state: State): ResetPwdFormState; // 添加获取 resetPwdFormState 的 Getter
};

export interface DecodedToken {
  iss: string;
  sub: string;
  aud: string;
  id: string;
  api_secret_id: string;
  name: string;
  account: string;
  email: string;
  mode: string;
  iat: number;
  exp: number;
}
